//
//  MYGHotNewsView.swift
//  MYDoctor
//
//  Created by Mo on 2024/3/6.
//

import UIKit

class MYGHotNewsView: UIView,UICollectionViewDelegate,UICollectionViewDataSource {

    var dataArr:[MYGNewsModel] = []
    override init(frame: CGRect) {
        super.init(frame: frame)
        getDatasource()
        let typeImageV = UIImageView()
        typeImageV.image = UIImage(named: "MYG_home_type")
        self.addSubview(typeImageV)
        typeImageV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(18)
            make.top.equalToSuperview().offset(15)
            make.width.equalTo(3)
            make.height.equalTo(15)
        }
        let typeNameLabel = UILabel()
        typeNameLabel.textColor = RGBA(r: 34, g: 34, b: 34, a: 1)
        typeNameLabel.font = UIFont.systemFont(ofSize: 21, weight: .medium)
        typeNameLabel.text = "热门新闻"
        self.addSubview(typeNameLabel)
        typeNameLabel.snp.makeConstraints { make in
            make.left.equalTo(typeImageV.snp.right).offset(10.5)
            make.centerY.equalTo(typeImageV)
            make.height.equalTo(29.5)
        }
        
        let allBtn = UIButton()
        allBtn.setImage(UIImage(named: "MYG_home_moreJ"), for: .normal)
        allBtn.setTitle("查看全部", for: .normal)
        allBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: .regular)
        allBtn.setTitleColor(RGBA(r: 96, g: 102, b: 114, a: 1), for: .normal)
        allBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 13)
        allBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: 79, bottom: 0, right: 0)
        allBtn.addTarget(self, action: #selector(clickAllbtn), for: .touchUpInside)
        self.addSubview(allBtn)
        allBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-14.5)
            make.centerY.equalTo(typeImageV)
            make.width.equalTo(85)
            make.height.equalTo(15)
        }
        
//        self.addSubview(topCollectionView)
        self.addSubview(newsCollectionView)
//        topCollectionView.snp.makeConstraints { make in
//            make.left.equalToSuperview().offset(18)
//            make.right.equalToSuperview().offset(-18)
//            make.top.equalTo(typeNameLabel.snp.bottom).offset(15)
//            make.height.equalTo(67)
//        }
        newsCollectionView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.top.equalTo(typeNameLabel.snp.bottom).offset(15)
            make.height.equalTo(SCALEWIDTH(520))
        }
    }
    
    @objc func clickAllbtn() {
        let vc = MYGIndInfoViewController()
        UIViewController.currentViewController?.navigationController?.pushViewController(vc, animated: true)
    }
    
    private func getDatasource(){
        MYGNetwork<MYGHomePageAPI, MYGPageModel<MYGNewsModel>>.request(target: .getIndInfoList(param: [
            "pageSize":"4",
            "page":"1","sort":"0","key": ""])) {[weak self] item in
                self?.dataArr.append(contentsOf: item.list)
                self?.topCollectionView.reloadData()
                self?.newsCollectionView.reloadData()
            } failed: { _, error in
                MMYGHUD.showToast(text: error)
            }
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if dataArr.count == 0{
            return 0
        }
        if collectionView == topCollectionView{
            return 3
        }else{
            return 4
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView == topCollectionView{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MYGNewsTopCollectionViewCell", for: indexPath) as! MYGNewsTopCollectionViewCell
            cell.modelDa = dataArr[indexPath.row]
            return cell
        }else{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MYGNewsContentCollectionViewCell", for: indexPath) as! MYGNewsContentCollectionViewCell
            cell.modelDa = dataArr[indexPath.row]
            return cell
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let item = dataArr[indexPath.row]
        let vc = MYGIndInfoDetailViewController()
        vc.id = item.id
        UIViewController.currentViewController?.navigationController?.pushViewController(vc, animated: true)

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var topCollectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 20
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.scrollDirection = .horizontal
        layout.minimumInteritemSpacing = 20
        layout.itemSize = CGSize(width:100, height: 67)
        let collV = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collV.showsVerticalScrollIndicator = false
        collV.showsHorizontalScrollIndicator = false
        collV.register(MYGNewsTopCollectionViewCell.self, forCellWithReuseIdentifier: "MYGNewsTopCollectionViewCell")
        collV.dataSource = self
        collV.delegate = self
        collV.backgroundColor = .clear
        return collV
    }()
    
    lazy var newsCollectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 15
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.minimumInteritemSpacing = 15
        layout.itemSize = CGSize(width: (screenWidth - 50)/2, height: SCALEWIDTH(253))
        let collV = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collV.showsVerticalScrollIndicator = false
        collV.showsHorizontalScrollIndicator = false
        collV.register(MYGNewsContentCollectionViewCell.self, forCellWithReuseIdentifier: "MYGNewsContentCollectionViewCell")
        collV.dataSource = self
        collV.delegate = self
        collV.backgroundColor = .clear
        collV.isScrollEnabled = false
        return collV
    }()
}
